/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package demo.handler;

import hapax.Template;
import hapax.TemplateDataDictionary;
import hapax.TemplateException;
import hapax.TemplateDictionary;
import hapax.TemplateLoader;
import hapax.TemplateResourceLoader;
import java.io.IOException;
import java.util.logging.Level;
import java.util.logging.Logger;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 *
 * @author root
 */
public class DemoServlet extends HttpServlet
{
//    private String greeting="Hello ";
//    public DemoServlet(){}
//    public DemoServlet(String greeting)
//    {
//        this.greeting=greeting;
//    }

    /**
     *
     * @param request
     * @param response
     * @throws ServletException
     * @throws IOException
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException
    {
        String name = "";
        
        if (request.getParameter("name") != null) {
            name = request.getParameter("name");
        } else {
            name = "World";
        }
        
        
        TemplateLoader templateLoader = TemplateResourceLoader.create(System.getProperty("user.dir"));
        Template template = null;
        System.out.println(System.getProperty("user.dir"));
        try {
            template = templateLoader.getTemplate("\\src\\demo\\view\\hello.xtm");
        } catch (TemplateException ex) {
            Logger.getLogger(DemoServlet.class.getName()).log(Level.SEVERE, null, ex);
        }
        TemplateDataDictionary dictionary = TemplateDictionary.create();
        dictionary.setVariable("name", name);
            
        String content = "abc abc abc";
        
        try {
            content = template.renderToString(dictionary);
        } catch (Exception e) {
            Logger.getLogger(DemoServlet.class.getName()).log(Level.SEVERE, null, e);
        }
        
        response.setContentType("text/html");
        response.setCharacterEncoding("UTF-8");
        response.setStatus(HttpServletResponse.SC_OK);
        response.getWriter().println(content);
//        response.getWriter().println("<h1>"+greeting+name+"</h1>");
//        response.getWriter().println("session=" + request.getSession(true).getId());
    }
    
    
}
